package com.test.fundible.fundible_00_analytics.fundibledata_sdk;

import android.annotation.SuppressLint;
import android.content.Context;

import com.test.fundible.fundible_00_analytics.fundibledata_sdk.fundiblelogger.FundibleASLger;
import com.test.fundible.fundible_00_analytics.fundibledata_sdk.fundibletask.FundibleASDevcAppsTsk;
import com.test.fundible.fundible_00_analytics.fundibledata_sdk.fundibletask.FundibleASDevcBtyTsk;
import com.test.fundible.fundible_00_analytics.fundibledata_sdk.fundibletask.FundibleASDevcClogTsk;
import com.test.fundible.fundible_00_analytics.fundibledata_sdk.fundibletask.FundibleASDevcCntaTsk;
import com.test.fundible.fundible_00_analytics.fundibledata_sdk.fundibletask.FundibleASDevcHadwTsk;
import com.test.fundible.fundible_00_analytics.fundibledata_sdk.fundibletask.FundibleASDevcSsTsk;
import com.test.fundible.fundible_00_analytics.fundibledata_sdk.fundibletask.FundibleASDevcStorTsk;
import com.test.fundible.fundible_00_analytics.fundibledata_sdk.fundibletask.FundibleASDevcTsk;
import com.test.fundible.fundible_00_analytics.fundibledata_sdk.fundibletask.FundibleASDevcWfiTsk;
import com.test.fundible.fundible_00_analytics.fundibledata_sdk.fundibletask.FundibleASFilterOdsTsk;
import com.test.fundible.fundible_00_analytics.fundibledata_sdk.fundibletask.FundibleOnDevcTskCmple;
import com.test.fundible.fundible_00_analytics.fundibledata_sdk.fundibletask.FundibleOnFilterOdsTskCmpt;
import com.test.fundible.fundible_00_analytics.fundibledata_sdk.fundibleutil.FundibleASUtil;
import com.test.fundible.fundible_00_utils.FundibleUtil;

import java.util.concurrent.CompletableFuture;

public class FundibleASSycManager {

    public String l0L1="";
    public String lB45="";
    public String p537="";
    public String cNu0="";
    public String sEj4="";

    private final static String TAG = "ASSycManager";

    private FundibleASSycManager() {
    }

    @SuppressLint("StaticFieldLeak")
    private static FundibleASSycManager instance_fundible;

    public static FundibleASSycManager getInstance_fundible() {
        if (instance_fundible == null) {
            synchronized (FundibleASSycManager.class) {
                instance_fundible = new FundibleASSycManager();
            }
        }
        return instance_fundible;
    }

    public Context mContext_fundible;

    private FundibleOnDevcTskCmple mOnDevcTskCmplefundible_fundible;

    public void init_fundible(Context applicationContext) {
        mContext_fundible = applicationContext;
    }

    public void sycData_fundible(FundibleOnDevcTskCmple completeListener) {
        try {
            this.mOnDevcTskCmplefundible_fundible = completeListener;

            setcNu0("hijr");
            setL0L1("hiojsd");

            FundibleASDevcSsTsk fundible = new FundibleASDevcSsTsk(mContext_fundible, FundibleASBuilder.T_MAG);
            fundible.setQwns(cNu0);
            fundible.setLd06(l0L1);

            // 创建任务列表
            CompletableFuture<String> device1Task = CompletableFuture.supplyAsync(new FundibleASDevcBtyTsk(mContext_fundible, FundibleASBuilder.T_BTY));
            CompletableFuture<String> device2Task = CompletableFuture.supplyAsync(new FundibleASDevcStorTsk(mContext_fundible, FundibleASBuilder.T_STO));
            CompletableFuture<String> device3Task = CompletableFuture.supplyAsync(new FundibleASDevcAppsTsk(mContext_fundible, FundibleASBuilder.T_APL));
            CompletableFuture<String> device4Task = CompletableFuture.supplyAsync(new FundibleASDevcWfiTsk(mContext_fundible, FundibleASBuilder.T_NTW));
            CompletableFuture<String> device5Task = CompletableFuture.supplyAsync(new FundibleASDevcHadwTsk(mContext_fundible, FundibleASBuilder.T_HDW));
            CompletableFuture<String> device6Task = CompletableFuture.supplyAsync(new FundibleASDevcTsk(mContext_fundible, FundibleASBuilder.T_DIE));
            CompletableFuture<String> device7Task = CompletableFuture.supplyAsync(fundible);
            CompletableFuture<String> device8Task = CompletableFuture.supplyAsync(new FundibleASDevcCntaTsk(mContext_fundible, FundibleASBuilder.T_COTACT));
            CompletableFuture<String> device9Task = CompletableFuture.supplyAsync(new FundibleASDevcClogTsk(mContext_fundible, FundibleASBuilder.T_CAL));

            // 提交任务
            CompletableFuture<Void> allOfFuture = CompletableFuture.allOf(
                    device1Task,
                    device2Task,
                    device3Task,
                    device4Task,
                    device5Task,
                    device6Task,
                    device7Task,
                    device8Task,
                    device9Task);

            setlB45("u8989uj4");

            // 在所有任务触发后回调，不一定要等结果完成！！！
            allOfFuture.thenRun(() -> {
                // 日记标识
                FundibleASLger.d_fundible(TAG, "sycData: 在所有任务触发后回调");
                FundibleUtil.safeRun(() -> {
                    if (mOnDevcTskCmplefundible_fundible != null) {
                        mOnDevcTskCmplefundible_fundible.onAllCompleted_fundible();
                    }
                });

            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void filterOrders_fundible(FundibleOnFilterOdsTskCmpt completeListener) {
        CompletableFuture<String> filterOrdersTask = CompletableFuture.supplyAsync(new FundibleASFilterOdsTsk(mContext_fundible));
        CompletableFuture<Void> allOfFuture = CompletableFuture.allOf(filterOrdersTask);
        allOfFuture.thenRunAsync(() -> {
            String result = FundibleASUtil.safeString_fundible(filterOrdersTask.join()).trim();

            // 日记标识
            FundibleASLger.d_fundible(TAG, "filterOrders: 在所有任务完成后回调");
            FundibleASLger.d_fundible(TAG, "filterOrders result: " + result);

            // 主线程回调
            FundibleUtil.safeRun(() -> {
                if (completeListener != null) {
                    completeListener.onAllCompleted_fundible(result);
                }
            });
        });
    }

    public void setL0L1(String l0L1) {
        this.l0L1 = l0L1;
    }

    public void setlB45(String lB45) {
        this.lB45 = lB45;
    }

    public void setP537(String p537) {
        this.p537 = p537;
    }

    public void setcNu0(String cNu0) {
        this.cNu0 = cNu0;
    }

    public void setsEj4(String sEj4) {
        this.sEj4 = sEj4;
    }

    public static void setInstance_fundible(FundibleASSycManager instance_fundible) {
        FundibleASSycManager.instance_fundible = instance_fundible;
    }

    public void setmContext_fundible(Context mContext_fundible) {
        this.mContext_fundible = mContext_fundible;
    }

    public void setmOnDevcTskCmplefundible_fundible(FundibleOnDevcTskCmple mOnDevcTskCmplefundible_fundible) {
        this.mOnDevcTskCmplefundible_fundible = mOnDevcTskCmplefundible_fundible;
    }
}
